local skel = fk.CreateSkill {
  name = "emo__shenyin",
  tags = {Skill.Limited},
}

Fk:loadTranslationTable{
  ["emo__shenyin"] = "神隐",
  [":emo__shenyin"] = "限定技，结束阶段，你可令一名角色获得本回合进入弃牌堆的不同牌名的各一张牌，这些牌进入弃牌堆时移出游戏直到你死亡。",

  ["#emo__shenyin-choose"] = "神隐：你可令一名角色获得本回合进入弃牌堆的不同牌名的各一张牌",
}

skel:addEffect(fk.EventPhaseStart, {
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Finish
    and player:usedSkillTimes(skel.name, Player.HistoryGame) == 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards, names = {}, {}
    room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if room:getCardArea(info.cardId) == Card.DiscardPile and table.insertIfNeed(names, Fk:getCardById(info.cardId).name) then
              table.insertIfNeed(cards, info.cardId)
            end
          end
        end
      end
    end, Player.HistoryTurn)
    if #cards == 0 then return false end
    local tos = room:askToChoosePlayers(player, {
      min_num = 1, max_num = 1, targets = room.alive_players, skill_name = skel.name,
      prompt = "#emo__shenyin-choose",
    })
    if #tos > 0 then
      event:setCostData(self, { tos = tos, cards = cards })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local cards = event:getCostData(self).cards
    if not cards then return end
    room:setPlayerMark(player, skel.name, cards)
    room:obtainCard(to, cards, false, fk.ReasonPrey, player, skel.name)
  end,
})

-- 进入弃牌堆废除
skel:addEffect(fk.AfterCardsMove, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    local mark = player:getMark(skel.name)
    if mark == 0 or player.dead then return false end
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(mark, info.cardId) then
            return true
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local mark = player:getMark(skel.name)
    if mark == 0 or player.dead then return false end
    local ids = {}
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(mark, info.cardId) then
            table.insertIfNeed(ids, info.cardId)
          end
        end
      end
    end
    ids = table.filter(ids, function (id)
      return room:getCardArea(id) == Card.DiscardPile
    end)
    if #ids > 0 then
      room:moveCardTo(ids, Card.Void, nil, fk.ReasonJustMove, skel.name, nil, false)
    end
  end,
})

-- 死亡时移除的牌塞回来
skel:addEffect(fk.Death, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:getMark(skel.name) ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local ids = table.filter(player:getTableMark(skel.name), function (id)
      return room:getCardArea(id) == Card.Void
    end)
    room:setPlayerMark(player, skel.name, 0)
    if #ids > 0 then
      room:moveCardTo(ids, Card.DiscardPile, nil, fk.ReasonJustMove, skel.name, nil, false)
    end
  end,
})

return skel
